package com.situ.company.score.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.situ.company.score.model.ScoreModel;
import com.situ.company.score.service.IScoreService;
import com.situ.company.score.service.impl.ScoreServiceImpl;
import com.situ.company.util.FmtRequest;

@WebServlet("/ScoreServlet")
public class ScoreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private IScoreService service = new ScoreServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html; charset=UTF-8");
		Object res = null;
		
		switch (req.getParameter("action")) {
		case "page":
			res = page(req);
			break;
		case "del":
			res = del(req);
			break;	
		case "add":
			res = add(req);
			break;
		case "get":
			res = get(req);
			break;
		case "upd":
			res = upd(req);
			break;
		
	}
		FmtRequest.write(resp.getWriter(), res);
	}
	
	private String upd(HttpServletRequest req) {
		ScoreModel model = new ScoreModel(); 
		model.setCodeEmp(req.getParameter("codeEmp"));
		model.setCodePro(req.getParameter("codePro"));
		model.setScore(req.getParameter("score"));	
		return service.update(model);
	}
	
	private Object get(HttpServletRequest req) {
		ScoreModel model = new ScoreModel(); 
		model.setCodeEmp(req.getParameter("codeEmp"));
		return service.selectModel(model);
	}
	
	private String del(HttpServletRequest req) {
		ScoreModel model = new ScoreModel();
		model.setCodeEmp(req.getParameter("codeEmp"));
		
		return service.delete(model);
	}

	private Object page(HttpServletRequest req) {
			ScoreModel model = new ScoreModel(); 
			model.setCodeEmp(req.getParameter("codeEmp"));
			model.setCodePro(req.getParameter("codePro"));
			model.setScore(req.getParameter("score"));
			String pageIndex = req.getParameter("pageIndex");
			String pageLimit = req.getParameter("pageLimit");
		    model.setPageIndex(Integer.parseInt(pageIndex));
			model.setPageLimit(Integer.parseInt(pageLimit));
			model.setPageOn(true);
			List<ScoreModel> list = service.selectList(model);
			Integer count = service.selectCount(model);
			Map<String,Object> map = new HashMap<>();
			map.put("list",list);
			map.put("count",count);
			return map;
		}

	private String add(HttpServletRequest req) {
		ScoreModel model = new ScoreModel();
		model.setCodeEmp(req.getParameter("codeEmp"));
		model.setCodePro(req.getParameter("codePro"));
		model.setScore(req.getParameter("score"));
		return service.insert(model);
	}
	
	
	
}
